package com.work.lxykeeper.module.pagemine;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;

import com.work.lxykeeper.R;
import com.work.lxykeeper.base.BaseActivity;
import com.work.lxykeeper.bean.LoginBean;
import com.work.lxykeeper.bean.PublicKeyData;
import com.work.lxykeeper.cache.Ckey;
import com.work.lxykeeper.cache.SPutils;
import com.work.lxykeeper.databinding.ActivityPagemineLoginBinding;
import com.work.lxykeeper.httpretrofit.Glide.GlideImgManager;
import com.work.lxykeeper.httpretrofit.MyCallback;
import com.work.lxykeeper.httpretrofit.PostBody;
import com.work.lxykeeper.httpretrofit.RetrofitManager;
import com.work.lxykeeper.module.main.MainZkyyActivity;
import com.work.lxykeeper.utils.JsonUtils;
import com.work.lxykeeper.utils.UIUtils;
import com.work.lxykeeper.utils.VerificationUtil;
import com.work.lxykeeper.utils.ViewUtils;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;


/**
 * author: Aly
 * date :2018/11/5 11:50
 * version:1.0
 * description:登陆页面
 */
public class LoginActivity extends BaseActivity {

    private ActivityPagemineLoginBinding binding;


    @Override
    protected View getLayoutView() {
        binding = ActivityPagemineLoginBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        mTitleBar.titleMiddle.setText("登录");
        Drawable dra = getResources().getDrawable(R.mipmap.close_error);
        dra.setBounds(0, 0, dra.getMinimumWidth(), dra.getMinimumHeight());
        mTitleBar.titleBack.setCompoundDrawables(dra, null, null, null);
        ViewUtils.setOnClickListeners(this, binding.btLogin, binding.tvForgetpassword, binding.tvNewuser, binding.ltContainer);
    }

    @Override
    protected void initData() {
        GlideImgManager.glideLoader(this, R.mipmap.logo, binding.ivLogo, 0);
        if (!TextUtils.isEmpty(SPutils.get(Ckey.PHONE))) {
            binding.etPhone.setText(SPutils.get(Ckey.PHONE));
        }
    }

    @Override
    protected void processClick(View v) {
        super.processClick(v);
        switch (v.getId()) {
            case R.id.bt_login:
                //登录
                HideKeyboard(binding.etPhone);
                if (VerificationUtil.isPhone(binding.etPhone.getText().toString()) && !TextUtils.isEmpty(binding.etPassword.getText().toString())) {
                    SPutils.put(Ckey.PHONE, binding.etPhone.getText().toString());
                    showLoaddialog("正在登录");
                    getPubkey(binding.etPhone.getText().toString(), binding.etPassword.getText().toString());
                }
                break;
            case R.id.tv_forgetpassword:
                //忘记密码
//                UIUtils.startActivity(ForgetPasswordActivity.class);
                Intent intent = new Intent(this, RegisterActivity.class);
                intent.putExtra("type", 1);
                UIUtils.startActivity(intent);
                break;
            case R.id.tv_newuser:
                //新用户注册
                Intent intent1 = new Intent(this, RegisterActivity.class);
                intent1.putExtra("type", 0);
                UIUtils.startActivity(intent1);
                break;
            case R.id.lt_container:
                //点击关闭软键盘
                HideKeyboard(binding.ltContainer);
                break;
        }
    }

    //获取公钥
    private void getPubkey(String phone, String password) {
        Call<PublicKeyData> call = RetrofitManager.getInstance().publicKeyData();
        call.enqueue(new MyCallback<PublicKeyData>() {
            @Override
            public void onSuccess(PublicKeyData response) {
                SPutils.put(Ckey.PUBKEY, response.getData().getData());
                long time = System.currentTimeMillis() - response.getData().getTime();
                SPutils.put(Ckey.TIME, time + "");
                login(phone, password);
            }

            @Override
            public void onFailure(int errorCode, String errMsg) {
                UIUtils.showToast(errMsg);
            }

            @Override
            public void onComplete() {

            }
        });
    }


    //登录
    private void login(String phone, String password) {
        Map<String, String> map = new HashMap<>();
        map.put("telphone", phone);
        map.put("password", password);
        map.put("time", (System.currentTimeMillis() - Integer.parseInt(SPutils.get(Ckey.TIME))) + "");
        Call<LoginBean> call = RetrofitManager.getInstance().userLogin(PostBody.toBody(map));
        call.enqueue(new MyCallback<LoginBean>() {
            @Override
            public void onSuccess(LoginBean response) {
                SPutils.put(Ckey.USERMESSAGE, JsonUtils.parseBeantojson(response));
                SPutils.put(Ckey.UID, response.getData().getId() + "");
                SPutils.put(Ckey.TOKEN, response.getData().getToken());
                SPutils.put(Ckey.AESKEY, response.getData().getKey());
                UIUtils.startActivity(MainZkyyActivity.class);
                finish();
            }

            @Override
            public void onFailure(int errorCode, String errMsg) {
                UIUtils.showToast(errMsg);
            }

            @Override
            public void onComplete() {
                hideLoaddialog();
            }
        });

    }
}
